const { createReport } = require('docx-templates');
const fs = require('fs');
const { mjpage } = require('mathjax-node-page');
const axios = require('axios');

const template = fs.readFileSync('template.docx');

// const stem = '<p>已知集合\(A=\{1,3,\sqrt{m}\}\),\(B=\{1,m\}\)，若\(A\cup B=A\)，则\(m=\)（&nbsp;&nbsp; ）</p>';

axios.post(
    'https://www.dykt.com/dou_practice/api/catalog_practice',
    {
        catalog_id: 30,
        top_id: 18
    }
).then(res => {
    // console.log('####', res.data.practices[0].stem);
    mjpage(res.data.practices[0].stem, {
        format: ["TeX"], // determines type of pre-processors to run
        fragment: true,
        output: 'mml',
    }, {mml: true, width: 100000, linebreaks: true }, output => {
        // console.log(output);
        createReport({
            template,
            data: {
                paper: {
                    title: '物理全国卷测试',
                },
                practice: {
                    stem: output
                },
            },
        }).then(buffer => {
            fs.writeFileSync('report.docx', buffer)
        });
    })
})

